package com.amoby.main.action.cbabill;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.amoby.base.common.BaseAction;
import com.amoby.base.common.Pager;
import com.amoby.main.entity.BsAccInfo;
import com.amoby.main.entity.SysUsers;
import com.amoby.main.service.CbaMgrService;

/**
 * @date 2015-7-6 下午3:12:31
 * @author zhaoyy
 * @version V1.0
 * @Description 商家&签约公司联邦银行账户管理(转账账户管理)
 */
@Controller
public class BsAccAction extends BaseAction {
    private static final Logger logger = LoggerFactory.getLogger(BsAccAction.class);
    @Autowired
    private CbaMgrService       cbaMgrService;

    @InitBinder
    protected void initBinder(WebDataBinder binder) throws Exception {
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    /**
     * 初始化页面
     * @author Lasing 2016-5-27 下午3:00:24
     * @param request
     * @param model
     * @return
     */
    @RequestMapping(value = "/menus/bsacc_mgr.html", method = RequestMethod.GET)
    public String indexPage(HttpServletRequest request, ModelMap model) {
        return AD_HTML + "cbabill/bsacc_mgr";
    }

    /**
     * 分页查询转账账户
     * @author Lasing 2016-5-27 下午3:01:26
     * @param request
     * @param model
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/bsacc/list.html")
    public Object dataGridList(HttpServletRequest request, ModelMap model) throws Exception {
        Pager pager = createPager(request);
        pager.addParam("bsName", getRequestParams(String.class, request, "bsName"));
        pager.addParam("account", getRequestParams(String.class, request, "account"));
        pager.addParam("bsType", getRequestParams(String.class, request, "bsType"));
        pager.addParam("payEnable", getRequestParams(Integer.class, request, "payEnable"));
        cbaMgrService.loadBsAccPage(pager);
        return getGridData(pager);
    }

    /**
     * 修改转账账户
     * @author Lasing 2016-5-27 下午3:36:03
     * @param item
     * @param request
     * @param model
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/bsacc/upd.html", method = RequestMethod.POST)
    public Object saveItem(BsAccInfo item, HttpServletRequest request, ModelMap model) throws Exception {
        Map<String, Object> result = getResultMap();
        try {
            SysUsers user = getUser();
            item = cbaMgrService.saveBsAcc(item, user);
            result.put(RESULT, true);
            result.put(MESSAGE, "保存成功");
        } catch (IllegalArgumentException e) {
            logger.error("操作失败，参数有误", e);
            result.put(RESULT, false);
            result.put(MESSAGE, "操作失败，输入参数有误！");
        } catch (DuplicateKeyException e) {
            logger.error("保存失败， 数据重复", e);
            result.put(RESULT, false);
            result.put(MESSAGE, "保存失败， 数据已存在！");
        } catch (Exception e) {
            logger.error("保存失败", e);
            result.put(RESULT, false);
            result.put(MESSAGE, "保存失败");
            result.put("exception", e.getMessage());
        }
        return result;
    }
}
